package com.pdg.mcplugin.misterdevtool;

import java.util.Iterator;
import java.util.Map;

import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.baseclasses.CommandProcessorBase;

public class CommandProcessor extends CommandProcessorBase<MisterDevtool>{
	private static final String COMMAND_INHAND = MDTFeatures.INHAND.toString();
	private static final String COMMAND_ENABLE = "ENABLE";
	private static final String COMMAND_DISABLE = "DISABLE";
	private static final String WARNING_INHAND_NOT_ENABLED = COMMAND_INHAND+" tool is not currently enabled.";
	public CommandProcessor(MisterDevtool thePlugin) {
		super(thePlugin, new String[] {
				"/mdt - shows help",
				"/mdt enable (tool name) - enables a tool",
				"/mdt disable (tool name) - disables a tool",
				"/mdt "+COMMAND_INHAND+" - activates the \""+COMMAND_INHAND+"\" tool"
		});
		this.setSubProcessor(COMMAND_ENABLE, new EnableCommandProcessor(getPlugin()));
		this.setSubProcessor(COMMAND_DISABLE, new DisableCommandProcessor(getPlugin()));
	}

	@Override
	protected boolean specialProcessCommand(CommandSender theCommandSender,
			String theSubCommand, ArgumentList theArgumentList) {
		if(theSubCommand.equalsIgnoreCase(COMMAND_INHAND)){
			return runInhandTool(theCommandSender);
		}
		return false;
	}

	private boolean runInhandTool(CommandSender theCommandSender) {
		if(!getPlugin().getFeatureManager().getFeature(MDTFeatures.INHAND)){
			getPlugin().sendWarningMessage(theCommandSender, WARNING_INHAND_NOT_ENABLED);
			return true;
		}
		if(theCommandSender instanceof Player){
			Player thePlayer = (Player)theCommandSender;
			ItemStack theItemStack = thePlayer.getItemInHand();
			if(theItemStack==null){
				theCommandSender.sendMessage("There is nothing in your hand.");
			}else{
				theCommandSender.sendMessage(String.format("Type: %s",theItemStack.getType().toString()));
				theCommandSender.sendMessage(String.format("Type ID: %d",theItemStack.getTypeId()));
				theCommandSender.sendMessage(String.format("Data: %d",theItemStack.getData().getData()));
				theCommandSender.sendMessage(String.format("Durability: %d",theItemStack.getDurability()));
				theCommandSender.sendMessage(String.format("Amount: %d",theItemStack.getAmount()));
				Map<Enchantment,Integer> theEnchantments = theItemStack.getEnchantments();
				Iterator<Enchantment> iterator = theEnchantments.keySet().iterator();
				while(iterator.hasNext()){
					Enchantment theEnchantment = iterator.next();
					theCommandSender.sendMessage(String.format("Enchantment: %1$s; Level: %2$d",theEnchantment.toString(),theEnchantments.get(theEnchantment)));
				}
				
			}
			return true;
		}else{
			theCommandSender.sendMessage("You cannot run the "+COMMAND_INHAND+" tool from the console!");
			return false;
		}
	}

}
